package com.bigShen.mybatis.official.services;

import com.bigShen.mybatis.official.dao.UserInfoMapper;
import com.bigShen.mybatis.official.entity.UserInfo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author : lei.shen
 * @date : 2018/11/21
 * @desc : 本例仅为demo,不考虑多并发,对SqlSessionFactory进行了重复创建;
 *      实际在应用运行期间, SqlSessionFactory应该只创建一次, sqlSession 每次查询应该创建一个新的
 */
public class UserBasicService {

    private static final String RESOURCE = "mybatis-config.xml";

    public static SqlSessionFactory initSessionFactory() {

        SqlSessionFactory sqlSessionFactory = null;
        try {
            InputStream is = Resources.getResourceAsStream(RESOURCE);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  sqlSessionFactory;
        //return SqlSessionFactorySingleton.getSqlSessionFactoryInstance();
    }

    @Deprecated
    public Object selctUserByName4Old() {
        List<UserInfo> list = new ArrayList<UserInfo>();
        SqlSession sqlSession = initSessionFactory().openSession();
        try {
            list = sqlSession.selectList("com.bigShen.mybatis.official.dao.UserInfoMapper.selectUserByName", "测试用户");
            //list = sqlSession.selectList("selectUser", "测试用户");
        } finally {
            sqlSession.close();
        }
         return  list;
    }

    public Object selectUserByName4New() {

        List<UserInfo> list = new ArrayList<UserInfo>();
        SqlSession sqlSession = null;
        try {
            InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            sqlSession = sqlSessionFactory.openSession();
            UserInfoMapper userInfoMapper = sqlSession.getMapper(UserInfoMapper.class);
            list = userInfoMapper.selectUserByName("测试用户");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }
        return list;
    }

    public Object selectUserByAccount() {

        List<UserInfo> userInfos = null;
        SqlSession sqlSession = null;

        SqlSessionFactory sqlSessionFactory = initSessionFactory();
        sqlSession = sqlSessionFactory.openSession();
        try {
            UserInfoMapper userInfoMapper = sqlSession.getMapper(UserInfoMapper.class);
            userInfos = userInfoMapper.selectUserByAccount("test001");
        } finally {
            sqlSession.close();
        }
        return  userInfos;
    }

    public static void main(String[] args) {

//        System.out.println(new UserBasicService().selctUserByName4Old());
//
//        System.out.println(new UserBasicService().selectUserByName4New());

        System.out.println(new UserBasicService().selectUserByAccount());

    }


}
